let Component = require('./Component');
let comDriver = require('@/common/driver');

class BaseControl extends Component{
    constructor(param = {}) {
        super(param);
    }

    getDefaultParam() {
        return Object.assign({}, super.getDefaultParam(), {
            id: comDriver.createID(),
            element: '',
            enabled: true,
            visible: true,
            value: null,
            text: null,
            checkers: [],
        });
    }

    createParam(param){
        super.createParam(param);
        if (!this.param.element) this.param.element = this.param.id;
    }

    async build(form){
        await super.build();
        this.param.form = form;
        this.band();
    }

    buildAfter(form){

    }

    band(){

    }

    beforGetValue(){

    }

    valueChanged(){

    }

    extract(){

    }

    display(){

    }

    get value(){
        this.beforGetValue();
        return this.param.value;
    }

    set value(value){
        this.param.value = value;
        this.valueChanged();
    }

    get text(){
        this.extract();
        return this.param.text;
    }

    set text(value){
        this.param.text = value;
        this.display();
    }

    get form(){
        return this.param.form;
    }

    get dom(){
        return this.param.form.element(this.param.element);
    }

    clear(){
        this.text = null;
        this.value = null;
    }

    async check() {
        if (this.param.checkers)
            for (let i = 0; i < this.param.checkers.length; i++) {
                let checker = this.param.checkers[i];
                checker.control = this;
                let data = await comDriver.app().core.runBean(checker.name, checker);
                if (!data.result) return false;
            }

        return true;
    }


}


module.exports = BaseControl;
